package com.gxa.servlet;

import com.alibaba.fastjson.JSON;
import com.gxa.dto.HouseDto;
import com.gxa.dto.ResponseResult;
import com.gxa.entity.House;
import com.gxa.exception.ErrorType;
import com.gxa.service.HouseService;
import com.gxa.service.impl.HouseServiceImpl;
import com.gxa.utils.WriteJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/house/*")
public class HouseServlet extends HttpServlet {
    @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req, resp);
        }

        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            String uri = request.getRequestURI(); // 获取uri
            String path = uri.substring(uri.lastIndexOf("/")); // 截取数据接口

            HouseService houseService = new HouseServiceImpl();
            if("/list".equals(path)){
                doQueryAllHouseByPage(request,response,houseService);
            } else if ("/addHouse".equals(path)){
                doAddHome(request,response,houseService);
            } else if ("/editHouse".equals(path)) {
                doEditHouse(request,response,houseService);
            } else if ("/delHouse".equals(path)) {
                doDelHouse(request,response,houseService);
            }

        }

    /**
     * 通过hid删除房产
     * @param request
     * @param response
     * @param houseService
     */
    private void doDelHouse(HttpServletRequest request, HttpServletResponse response, HouseService houseService) {
        // 1、获取参数
        String hid = request.getParameter("hid");
        System.out.println("hid====>" + hid);
        // 2、
        boolean delHouseById = houseService.delHouseById(hid);
        if (delHouseById){
            ResponseResult<Object> result = new ResponseResult<>(ErrorType.SUCCESS, 0, null);
            WriteJson.writeJson(result,response);
        } else{
            ResponseResult<Object> result = new ResponseResult<>(ErrorType.FAILED_SAVE_PY_RESULT, 0, null);
            WriteJson.writeJson(result,response);
        }
    }

    /**
     * 修改房产
     * @param request
     * @param response
     * @param houseService
     */
    private void doEditHouse(HttpServletRequest request, HttpServletResponse response, HouseService houseService) {
        House house = null;
        try {
            BufferedReader reader = request.getReader();
            String readLine = reader.readLine(); //
            //String toJSONString = JSON.toJSONString(readLine);
            //System.out.println(toJSONString);
            house = JSON.parseObject(readLine, House.class);
            System.out.println(house);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 2、调用service
        boolean editHouseResult = houseService.editHouse(house);
        if (editHouseResult){
            ResponseResult result = new ResponseResult(ErrorType.SUCCESS,0,null);
            WriteJson.writeJson(result,response);
        } else {
            ResponseResult result = new ResponseResult(ErrorType.FAILED_SAVE_PY_RESULT,0,null);
            WriteJson.writeJson(result,response);
        }
    }

    /**
     * 新增房产
     * @param request
     * @param response
     * @param houseService
     */
    private void doAddHome(HttpServletRequest request, HttpServletResponse response, HouseService houseService) {
        // 1、获取参数

       /* String propertyId = request.getParameter("propertyId");
        String houseNum = request.getParameter("houseNum");
        String count = request.getParameter("count");
        String liveTime = request.getParameter("liveTime");
        // 小区：
        String cid = request.getParameter("cid");
        System.out.println("cid===>" + cid);
        String cname = request.getParameter("cname");
        System.out.println("cname===>" + cname);

        // 栋数
        String bid = request.getParameter("bid");
        System.out.println("bid===>" + bid);
        String bname = request.getParameter("bname");
        System.out.println("bname===>" + bname);
        // 单元
        String unit = request.getParameter("unit");

        // 业主
        String oid = request.getParameter("oid");
        System.out.println("oid===>"+ oid);
        String oname = request.getParameter("oname");
        System.out.println("oname===>"+oname);*/
        HouseDto houseDto = null;
        try {
            BufferedReader reader = request.getReader();
            String readLine = reader.readLine(); //
            //String toJSONString = JSON.toJSONString(readLine);
            //System.out.println(toJSONString);
            houseDto = JSON.parseObject(readLine, HouseDto.class);
            System.out.println(houseDto);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 2、调用service
        boolean addHouseResult = houseService.addHouse(houseDto);
        if (addHouseResult){
            ResponseResult result = new ResponseResult(ErrorType.SUCCESS,0,null);
            WriteJson.writeJson(result,response);
        } else {
            ResponseResult result = new ResponseResult(ErrorType.FAILED_SAVE_PY_RESULT,0,null);
            WriteJson.writeJson(result,response);
        }
    }

    /**
     * 分页查询所有的房产
     * @param request
     * @param response
     * @param houseService
     */
    private void doQueryAllHouseByPage(HttpServletRequest request, HttpServletResponse response, HouseService houseService) {
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");

        String searchParams = request.getParameter("searchParams");
        HouseDto houseDto = JSON.parseObject(searchParams, HouseDto.class);
        System.out.println(houseDto);

        List<House> houseList = houseService.queryHouseByHouseDto(page,limit,houseDto);
        Integer count = houseService.count(houseDto);

        ResponseResult result = new ResponseResult(0,"success",count,houseList);

        WriteJson.writeJson(result,response);
    }


}
